﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SongSharp.Domain.Objects;
using SongSharp.Domain.Playlisting.M3U;

namespace SongSharp.Appliance
{
    public static class LibraryApp
    {
        public static void AddPlaylistsToLibrary(string[] playlists)
        {
            List<Playlist> lists = new List<Playlist>();
            List<string> checkedList = null;

            if (playlists.Length > 0)
            {
                checkedList = FilterToExtension(playlists.ToList(), ".m3u");

                foreach (string playlist in checkedList)
                {
                    Playlist p = M3UReader.CreateFromFile(playlist, Path.GetFileNameWithoutExtension(playlist), false);
                    Playlist.Update(p);
                }
            }
        }

        public static List<Track> AddTracksToLibrary(string[] tracks)
        {
            List<Track> list = new List<Track>();
            List<string> checkedList = null;
            if (tracks.Length > 0 && SongSharpApp.States.CurrentPlaylist != null)
            {
                switch (SongSharpApp.States.CurrentPlaylist.PlaylistType)
                {
                    case Playlist.AutoGeneratedPlaylistType.All_music:
                        // Saves tracks in the library
                        checkedList = FilterToExtension(tracks.ToList(), ".mp3");
                        checkedList = RemoveDuplicates(checkedList);

                        foreach (string s in checkedList)
                            list.Add(new Track(s));

                        Track.SaveTracks(list);
                        break;
                    case Playlist.AutoGeneratedPlaylistType.Now_playing:
                        // Adds them just for playing. Tracks are not saved in the library
                        checkedList = FilterToExtension(tracks.ToList(), ".mp3");
                        foreach (string s in checkedList)
                            list.Add(new Track(s, true));
                        QueueApp.EnqueueRange(list);
                        break;
                }
                return list;
            }
            return null;
        }

        private static List<string> RemoveDuplicates(List<string> inputList)
        {
            List<string> checkedList = new List<string>();
            List<Track> savedTracks = Track.GetAll();

            Dictionary<string, int> uniqueList = new Dictionary<string, int>();
            List<string> outputList = new List<string>();

            foreach (string currValue in inputList)
            {
                if (!uniqueList.ContainsKey(currValue))
                {
                    uniqueList.Add(currValue, 0);
                    outputList.Add(currValue);
                }
            }

            foreach (string track in outputList)
            {
                if (savedTracks.Where(t => t.Url == track).Count() == 0)
                {
                    checkedList.Add(track);
                }
            }

            return checkedList;
        }

        private static List<string> FilterToExtension(List<string> inputList, string extension)
        {
            List<string> outputList = new List<string>();

            foreach (string s in inputList)
            {
                if (Directory.Exists(s))
                {
                    string[] files = Directory.GetFiles(s, "*.mp3", SearchOption.AllDirectories);
                    outputList.AddRange(files.ToList());
                }
                else if (Path.GetExtension(s).ToLower() == extension.ToLower())
                {
                    outputList.Add(s);
                }
            }

            return outputList;
        }
    }
}


